import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const TableComponent 		= $Lazy.state(()=>import('./cpt/table-component'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
// const Export 				= $Lazy.file(()=>import('#private/common/export'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '客户名称',			name: 'hosp_id', 					type: 'select', loading:true },
			{ label: '实验科室',			name: 'project_parent_id', 			type: 'select', loading:true, idStr:'id' },
			{ label: '区域',				name: 'region_id', 					type: 'select', loading:true },
			{ label: '业务员',			    name: 'salesman_id', 				type: 'select', loading:true },
			{ label: '接收标本时间',		names: ['start_at','end_at'], name:'jz-time', 	type:'date-range', format:1 },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
			{ label: '状态',				name: 'status',						type: 'select', loading:true, idStr:'code' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}else{
				this.props?.onKeyChange?.()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 客户名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital_adm' } ) ) 
			// 实验科室
			$fn.setRow(forms, 'project_parent_id', await f.getCache({ api:'shi_yan_ke_shi' } ) ) 
			// 区域
			const area = await f.getCache({ api:'area_adm' } )
			$fn.setRow(forms, 'region_id', area )
			// 状态
			$fn.setRow(forms, 'status', await f.getDicCache({ code:45300 } ) )
			// 业务员
			// $fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			const isSalesman = await f.getCache({ api:'salesman_adm' } )
			$fn.setRow(forms, 'salesman_id', $fn.isObject(isSalesman) ? [] : isSalesman)
			$fn.hideRows(forms, 'salesman_id', $fn.isObject(isSalesman) ? true : false)
			// 如果是单个数据直接展示 2021-10-20 5:04:47 ?F10: PM? --伍智睿
			if (isSalesman.length === 1) {
				this.searchFormRef.setValue({salesman_id: isSalesman[0].value})
			} else if (area.length === 1) {
				this.searchFormRef.setValue({region_id: area[0].value})
			}
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill-st/businessConstSummary',{isIndex:true, callback: result => this.setState({ result })})
	
	// 表单
	cols = [
		{ title: '序号',				dataIndex: 'sort', 						...$fn.table.num },
		{ title: '岗位',				dataIndex: 'project_name', 				width:150 },
		{ title: '客户名称',				dataIndex: 'hosp_name', 				width:250 },
		{ title: '项目名称',				dataIndex: 'kind_name', 				width:250 },
		{ title: '数量',				dataIndex: 'sum_spec_in', 				...$fn.table.num },
		{ title: '冲抵数量',				dataIndex: 'sum_spec_out', 				...$fn.table.num },
		{ title: '实际数量',				dataIndex: 'sum_spec', 					...$fn.table.num },
		{ title: '标准价格',				dataIndex: 'sum_sprice', 				...$fn.table.num },
		{ title: '实际价格',				dataIndex: 'sum_price', 				...$fn.table.num },
		{ title: '送检科室',				dataIndex: 'project_parent_name', 		width:150 },
		{ title: '外送单位',				dataIndex: 'outsourcing_company', 		width:150 },
		{ title: '开票单位（NC）',		dataIndex: 'organization_name', 		width:150 },
		{ title: '开票单位编码（NC）',	dataIndex: 'organization_code', 		width:150 },
		{ title: '业务员',				dataIndex: 'salesman_user', 			...$fn.table.name },
		{ title: '业务员编码',			dataIndex: 'salesman_code', 			width:150 },
		{ title: '区域',				dataIndex: 'region_name', 				width:150 },
		{ title: '区域编码',				dataIndex: 'region_num', 				...$fn.table.num }
	]
	
	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this, 'bill-st/businessConstSummary')						// 导出
		]
	}

    // exports = () => {
	// 	Export.then(f=>f.call(this, 'bill-st/parentConstSummary'))
	// }
		
	render(){
		const { result } = this.state
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this, { timeStr:'1-today' }) }
				{/* 表格 */}
				<div className='bcf fv ex'>
					{ Table.call(this,{type:1, isIndex:true, isInit: window.isInit,
						onRow:( rows ) => {
							const jzTime = this.searchFormRef.getName('jz-time')
							const timeNull = this.searchFormRef.getName('time-null')
							const times = {
								start_at: jzTime[0],
								end_at: jzTime[1],
								bill_start_at: timeNull ? timeNull[0] : '',
								bill_end_at: timeNull ? timeNull[1] : ''
								}
							this.tableRef.load({ ...rows, ...times })
						},
						suffix: (
							<>
								<div className='ml10 g9'>总数量 <b className='f14 c0'>{result?.total_sum_spec_in}</b></div>
								<div className='ml10 g9'>总冲抵数量 <b className='f14 c0'>{result?.total_sum_spec_out}</b></div>
								<div className='ml10 g9'>总实际数量 <b className='f14 c0'>{result?.total_sum_spec}</b></div>
							</>
						)
					 }) }
				</div>
				{/* 表格 */}
				<div className='bcf fv mt5 ex pt10'>
					<TableComponent onRef={ ref => this.tableRef = ref }/>
				</div>
			</>
		)
	}
}

export default Index